(**********************************************************************

    :Program.    MemSystem.def
    :Contents.   convenient memory allocation procedures
    :Author.     Nicolas Benezan [bne]
    :Address.    Postwiesenstr. 2, D7000 Stuttgart 60
    :Phone.      711/333679
    :Copyright.  Public Domain
    :Language.   Modula-2
    :Translator. M2Amiga AMSoft 3.11
    :Imports.    ErrorReq,TaskMemory [bne]
    :History.    V1.0 [bne] 17.Jun.88 (private)
    :History.    V1.1 [bne] 09.Jul.88 (+ TaskMem)
    :History.    V1.2 [bne] 12.Jan.89 (ErrorReq items excluded)
    :History.    V1.3 [bne] 04.Mar.89 (+ NoCareDeallocate(), Levels )

**********************************************************************)

DEFINITION MODULE MemSystem;

FROM SYSTEM     IMPORT ADDRESS;

TYPE    LevelKey; (* opaque to avoid manipulation *)

VAR     minMemory:LONGINT; (* Default=20kB *)
        Hysteresis:LONGINT;(* Default=30kB *)

PROCEDURE Allocate(VAR Pointer:ADDRESS;Size:LONGINT);
(*:Input.       Size: Amount of required memory (Bytes)
  :Output.      Pointer: Address of the allocated memory or
  :Output.      NIL if the allocation failed
  :Semantic.    Allocates memory - Requester is brought up if there's
  :Semantic.    not enough
  :Note.        appends an entry to the memEntry-list of the task
  :Note.        the allocated memory is freed automatically at
  :Note.        Task termination (RemTask() )
*)

PROCEDURE AllocMem(VAR Pointer:ADDRESS;Size:LONGINT;Chip:BOOLEAN);
(*:Input.       Size: Amount of required memory (Bytes)
  :Input.       Chip: TRUE: ChipMem required FALSE: doesn't matter which
  :Output.      Pointer: Address of the allocated memory or
  :Output.      NIL if the allocation failed
  :Semantic.    Allocates memory - Requester is brought up if there's
  :Semantic.    not enough
  :Note.        Appends an entry to the memEntry-list of the task
*)

PROCEDURE Deallocate(VAR Pointer:ADDRESS);
(*:Input.       Pointer: Address of the memory to be deallocated
  :Output.      Pointer is set to NIL
  :Semantic.    Deallocates Memory blocks allocated with
  :Semantic.    MemSystem.Allocate() or MemSystem.AllocMem()
  :Note.        Breaks if already free memory is deallocated
*)

PROCEDURE NoCareAllocate(VAR Pointer:ADDRESS;Size:LONGINT);
(*:Semantic.    The same as Allocate() but this one would rather
  :Semantic.    terminate the Programm than it would return NIL
*)

PROCEDURE NoCareAllocMem(VAR Pointer:ADDRESS;Size:LONGINT;Chip:BOOLEAN);
(*:Semantic.    The same as AllocMem() but this one would rather
  :Semantic.    terminate the Programm than it would return NIL
*)

PROCEDURE NoCareDeallocate(VAR Pointer:ADDRESS);
(*:Input.       Pointer: Address of the memory to be deallocated
  :Output.      Pointer is set to NIL
  :Semantic.    Deallocates Memory blocks allocated with
  :Semantic.    MemSystem.Allocate() or MemSystem.AllocMem()
  :Note.        Does not complain about freeing memory twice
*)

PROCEDURE EnterLevel(VAR Level:LevelKey);
(*:Output.      Level: Key assigned to the new level
  :Semantic.    Creates a new level. (See also ExitLevel() )
  :Note.        The first level is created automatically
  :Note.        during startup, so you needn't EntrerLevel()
  :Note.        at the beginning of your program.
*)

PROCEDURE ExitLevel(VAR Level:LevelKey);
(*:Input.       Level: Key assigned by EnterLevel()
  :Semantic.    Deallocates all memory allocated in this
  :Semantic.    or deeper levels.
  :Note.        Notice that each ExitLevel() must follow a corresponding
  :Note.        EnterLevel, whereas not every EnterLevel() must have
  :Note.        a corresponding ExitLevel()
*)

END MemSystem.
